Okay.
So today we're going to finish off looking at loops.
So to start off with add a script I'm going to add it into Server Script service this time.
And I'm going to add in a part with a decal.
Inside of it.
Okay, I'm going to reference both the part and the local part is equal to game dot workspace dot part.
Because we put our script in the server script service, we have to reference it using game Dot.
We could use script dot, but it would be much take a lot more time to do.
Then local decal is equal to part dot decal.
Okay.
Um, okay.
So um, let's go into the toolbox and find an image to use for this decal.
Okay.
Let's just click about anyone.
Don't let's go for this first one here.
Um, doesn't matter too much which image you use.
It's more of a coding we're looking at rather than the actual looks of the image.
And let's put it into our decals texture property there, and we can probably have a look at it in the
game.
We can see it's just a bit stretched.
So let's, um, move it around a bit.
So as not as stretched.
Perfect.
Okay.
So, um, we're going to have a look at a bit of an example about how you can combine loops.
So loops and statements are very useful, but they become even more useful when you connect them together
okay.
Now usually you can connect most loops together completely fine.
And there'll be no issues.
Um, however, sometimes when you connect them together, there will be a slight issue which you may
have to involve breaking a loop, which we'll look at how to do today as well.
Um, so let's say for this decal, okay.
It's face is currently at the top.
Okay.
Um, let's say we only want this we only we only want this code to work if the decals face is at the
top face.
Because if it's if it's at any other face, you wouldn't really be able to see it from where it is.
Okay.
So let's use an if loop to check that.
So an if statement to check that.
Um, so to do that we'd say if decal we're going to look at the face property.
Now face is an enum value okay.
It's an enum datatype.
And we use enum dot normal id for this one.
We haven't looked at this before but it's enum dot normal id is equal equal enum dot normal id dot.
And then we can pick top is the one we want to look at for that okay.
So um if this happens.
So if the face is at the top, what I'd like to happen is I would like to slowly make this decal transparent.
Okay.
And the best way to do that would be using a for loop.
Okay.
So we can say for 100 times, um, add a small amount onto the transparency until afterwards.
100 times have gone up.
It's been completely invisible.
Okay.
Um, we the best way to do that one.
So let's write a for loop.
Okay.
So for I is equal to we want this to go to transparency from 0 to 1.
So we could say one comma ten okay.
Which would mean it adds ten values on.
But it would be quite blocky because you'd be jumping from 0.1 to 0 point 2 to 0.3, 2.4.
So I'm going to do it 100 times.
So it's adding smaller values.
So it's more of a gradual um, it becomes less visible gradually rather than in big jumps.
Okay comma one do.
Okay.
So each time I want to increase the value of a transparency.
So decal dot transparency is equal to decal dot transparency plus 0.01 okay.
Because if you times 0.01 by 100 you get one.
And that's what we want the transparency to end up as as one okay.
Let's wait as well a very small amount of time.
Let's say 0.01 seconds.
Um, just so we can see what's happening or we put a change to 0.1.
So a bit more time to do other stuff.
Okay.
Um, so this is an example of a loop inside of a statement.
Okay.
We have our statement here and we have our for loop here.
Okay.
So you might notice that the the for loop has all been shifted one to the right.
And it does that automatically.
The reason why is just for readability reasons.
So it's much more readable and easier to see.
But the for loop is inside the if statement because it's shifted one to the right.
It's actually looks like it's inside, just like how in the workspace the cow is shifted one to the
right of the part because it's inside of it.
Okay.
And, um, anything you write inside of your for loop will be auto shifted to one to the right.
So don't try and undo that.
Okay.
Leave it as it is because it's meant to be like that.
And trust me, it will make your code much more readable, especially when you have, um, a loop inside
a statement.
Inside another statement, inside of a loop.
It becomes very difficult to keep track, especially when you have all these ends at the at the end
of it as well.
So it's a very good idea to keep it as it is.
Okay, so let's try this out by running the game.
And hopefully it should start to make our, um, the cow invisible.
You can see it's very slowly happening there.
Part.
Do you see it increasing transparency value until it reaches one?
Okay, I think I'm going to change the parts transparency to one as well so I can see it a bit easier.
I'll have to change that.
Not while I'm playing the game.
Okay.
Um, but let's say something happens, um, this time.
Okay.
So we select this.
So it's increasing transparency, but let's say suddenly the face changes to be at the bottom okay.
So it's now changed face.
But you could see it didn't stop the transparency changing the transparency to one.
So what happens here is that um this when it started the decals face was at the top.
So this if statement works completely fine.
Then it basically got caught up inside of this for loop.
So as soon as it read this bit here, it's now staying here until it's finished.
This entire thing.
So even though the face changed, it can't do anything about it because it's inside this for loop.
And there's very little ways to stop this for loop from running.
Okay, um, a possibility could be writing an if statement inside a for loop and saying checking again
if the face is equal to enum dot normal id dot top like that's there.
And then putting our stuff inside here.
So but it only does it check it checks it 100 times.
Um which is a bit overkill.
Um, and it also means it won't actually stop the for loop, because if the DeKalb face property does
not equal to the top, then it will, it will, it will keep running the for loop, but it just won't
go.
It just won't.
Um, do do our code here so it doesn't really solve the problem.
Unfortunately, it doesn't stop it from happening.
It just kind of stops any visual signs of it happening.
So we need a different way to do this.
And we can do that by breaking the for loop.
So adding a break in your for loop will basically mean that the for loop will stop running.
So it will exit outside of the for loop.
And that can be done for all kinds of reasons.
But this is a key example about when you might want to use it, when it's kind of stuck inside it,
especially if long for loop, which maybe might take might take a few minutes to run.
You're going to want to give it a lot of time.
It can take a lot of time to run all the way through.
So if you do want to break fats, it becomes a lot more difficult as well.
So, um, to add a break in, you put a break inside of an if statement.
Okay.
So let's try our our if statement.
Let's say oh add caps capital letters on there.
If decal dot face is equal equal enum dot normal id dot top okay.
So if the decal was face property is at the top, well then if it is oh sorry.
If it if it isn't in this case.
So if it is not equal to the top.
So if it's face changes okay.
We want to stop this for loop from happening any longer.
And to do that it's very simple.
It's just one word called break like art fair okay.
And that will stop it from running okay.
So let's go back into our game and press play and or press one.
Sorry.
And let's have a look.
So let's change the property here to bottom.
And it stops there.
So it stops it from going on any further okay.
So this is a really good example about how breaks are so important.
A probably more of a useful example might be if you have a game with a timer telling you how long you've,
um, how long it's taking for you to, to complete a course, for example, and let's say an obstacle
course.
And let's say you die on this obstacle course.
Well, you'd want the timer to stop.
Um, it wouldn't make sense for it to keep going, but let's say it's in a for loop, and the for loop
lasts.
Um, I don't know, 100 seconds, for example.
Well, in that case, you'd want to break it using this method here.
Okay.
So breaking um, breaking a loop so you don't always have to do it is this specific case where you want
a for loop to stop running.
Okay.
But it's useful to think about anyway okay.
And it also gives you an example about how you can put loops inside of statements.
They can kind of work together like that okay.
So that is loops finished okay.
So join me in the next lesson where we will be continuing looking at some other areas of coding in Roadblock
Studio.